package com.af.monitorModule.generator.service.impl;

import com.af.monitorModule.common.ResultEnum;
import com.af.monitorModule.generator.entity.Monitor;
import com.af.monitorModule.generator.entity.Robot;
import com.af.monitorModule.generator.entity.VO.MonitorVO;
import com.af.monitorModule.generator.entity.VO.RobotVO;
import com.af.monitorModule.generator.mapper.RobotMapper;
import com.af.monitorModule.generator.service.RobotService;
import com.af.monitorModule.response.exception.CustomerException;
import com.af.monitorModule.util.RestUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lyz_4dou
 * @since 2021-05-11
 */
@Service
public class RobotServiceImpl extends ServiceImpl<RobotMapper, Robot> implements RobotService {

    @Autowired
    private RobotMapper robotMapper;

    @Override
    public JSONObject findRobot(String page, String limit, String search) {
        JSONObject condition = JSONObject.parseObject(search);
        Page<RobotVO> p = new Page<RobotVO>(Integer.parseInt(page), Integer.parseInt(limit));
        QueryWrapper<Robot> wrapper = new QueryWrapper<>();
        if (condition != null)
            condition.forEach((k,v)->{
                if (!"".equals(v.toString().trim())){
                    if (k.contains("name")){
                        wrapper.like(k,v.toString());
                    }else {
                        wrapper.eq(k,v.toString());
                    }

                }
            });
        IPage<RobotVO> robot = robotMapper.findRobot(p, wrapper);
        JSONObject res = new JSONObject();
        List<RobotVO> records = robot.getRecords();
        CountDownLatch countDown = new CountDownLatch(records.size());
        for (int i = 0; i < records.size(); i++) {
            int finalI = i;
            new Thread(()->{
                RobotVO one = records.get(finalI);
                one.setAlive(RestUtil.checkUrl(one.getRobotRequest(),2,"GET"));
                countDown.countDown();
            }).start();
        }
        try {
            countDown.await();
        } catch (InterruptedException e) {
            throw new CustomerException(ResultEnum.ASYNC_CHECK_ERROR);
        }
        res.put("data",records);
        res.put("code",0);
        res.put("count",records.size());
        res.put("msg","机器人列表");
        return res;
    }

    @Override
    public JSONObject updateRobot(Robot robot, String id) {
        if (robot == null || id == null){
            return ResultEnum.PARAM_INCORRECRT.getResult();
        }
        robot.setId(Integer.valueOf(id));
        robotMapper.updateById(robot);
        return ResultEnum.OK.getResult();
    }

    @Override
    public JSONObject add(Robot robot) {
        if (robot == null){
            return ResultEnum.PARAM_INCORRECRT.getResult();
        }
        if (robotMapper.insert(robot) > 0){
            return ResultEnum.OK.getResult();
        }else {
            return ResultEnum.CREATE_ERROR.getResult();
        }
    }
}
